<template>
  <div>
    <el-dialog
      class="title"
      :title="{ add: '新增企业', edit: '编辑' }[mode]"
      :visible.sync="isShow"
      style="text-align: center"
      @close="closeEvent"
    >
      <el-form ref="form" label-width="100px" :model="form" :rules="rules">
        <el-form-item label="企业编号" prop="eid">
          <el-input v-model.trim="form.eid" />
        </el-form-item>
        <el-form-item label="企业名称" prop="name">
          <el-input v-model.trim="form.name" />
        </el-form-item>
        <el-form-item label="企业简称" prop="short_name">
          <el-input v-model.trim="form.short_name" />
        </el-form-item>
        <el-form-item label="企业简介" prop="intro">
          <el-input v-model.trim="form.intro" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model.trim="form.remark" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: center">
          <el-button @click="isShow = false">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { enterpriseAdd, enterpriseEdit } from '@/api/enterpriseList'
export default {
  data() {
    return {
      mode: '', // add:新增  edit:编辑
      isShow: false,
      form: {
        eid: '',
        name: '',
        short_name: '',
        intro: '',
        remark: '',
        status: ''
      },
      rules: {
        eid: [{ required: true, message: '必填', trigger: 'change' }],
        name: [{ required: true, message: '必填', trigger: 'change' }],
        short_name: [{ required: true, message: '必填', trigger: 'change' }],
        intro: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods: {
    // 确定按钮点击
    submit() {
      // console.log('点击了确定')
      this.$refs.form.validate(async(result) => {
        console.log(result)
        // 调用成功
        /*
        1. 提示
        2. 关闭弹框
        3. 清空表单验证和还原表单数据
        4.刷新父级列表数据
        */
        if (result) {
          // 判断是新增还是编辑
          if (this.mode === 'add') {
            await enterpriseAdd(this.form)
            this.$message.success('新增成功')
          } else if (this.mode === 'edit') {
            await enterpriseEdit(this.form)
            this.$message.success('编辑成功')
          }
          this.isShow = false
          this.$emit('getData')
        }
        console.log(result)
      })
    },
    // 关闭时清空表单验证与重置表单数据
    closeEvent() {
      this.$refs.form.resetFields()
      this.isShow = false
      this.form = {
        eid: '',
        name: '',
        short_name: '',
        intro: '',
        remark: '',
        status: ''
      }
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__header {
  background-color: #1394fa;
  .el-dialog__title {
    color: #fff;
  }
}
</style>
